<?php
namespace app\model;

use think\Model;

class GameProductModel extends Model
{
    protected $table = 'game_product';
    protected $pk = 'id';

    // 数据库字段类型映射（关键修复）
    protected $type = [
        'version' => 'integer', // 数据库version为int(11)
        'rating' => 'float',
        'popularity' => 'integer',
        'is_offline' => 'integer',
        'is_published' => 'integer',
        'app_id' => 'string',
    ];


     // 新增：获取最新SKU
     public function latestSku()
     {
         return $this->skus()->find();
     }

    // 关联关系优化（明确外键和主键）
    public function features()
    {
        return $this->hasMany(GameProductFeatureModel::class, 'product_id', 'id')
            ->order('sort'); // 增加排序
    }

    public function categories()
    {
        return $this->hasMany(GameProductCategoryModel::class, 'product_id', 'id');
    }

    public function genres()
    {
        return $this->hasMany(GameProductGenreModel::class, 'product_id', 'id');
    }

    public function screenshots()
    {
        return $this->hasMany(GameProductScreenshotModel::class, 'product_id', 'id');
    }

    public function accounts()
    {
        return $this->hasMany(GameAccountModel::class, 'product_id', 'id');
    }

    public function skus()
    {
        return $this->hasMany(GameProductSkuModel::class, 'product_id', 'id')
            ->order('id DESC'); // 默认最新SKU
    }

    

}